import { createStore, createLogger } from 'vuex';
import getters from './getters'

// const files = require.context('./modules', false, /\.js$/)
let modules = {}
const files = import.meta.globEager("./modules/*.js")
for (const path in files) {
  const moduleName = path.replace(/(.*\/)*([^.]+).*/gi, '$2')
  modules = {...modules,[moduleName]:files[path].default}
}

// files.keys().forEach((key) => {
//   modules[key.replace(/(\.\/|\.js)/g, '')] = files(key).default
// })

// Object.keys(modules).forEach((key) => {
//   modules[key]['namespaced'] = true
// })

const debug = import.meta.env.NODE_ENV !== 'production'
// console.log(555, modules)
export const store = createStore({
  modules,
  getters,
  strict: debug,
  plugins: debug ? [createLogger()] : []
})

export function useStore() {
  return store;
}

export function loadStore(app) {
  app.use(store)
  return store;
}

export default store;